package vcp.client.GUI.mvc.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.regex.Pattern;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JLayer;
import javax.swing.JPanel;
import javax.swing.text.JTextComponent;

import org.joda.time.DateTime;

import vcp.client.GUI.events.ValidationDescriptionEvent;
import vcp.client.GUI.mvc.controller.UIRootController;
import vcp.client.GUI.mvc.view.custom.CustomInputVerifier;
import vcp.client.GUI.mvc.view.custom.CustomJSpinnerDate;
import vcp.client.GUI.mvc.view.effects.ValidationLayerUI;
import vcp.client.interfaces.listeners.IValidationListener;
import vcp.client.utility.Common;
import vcp.client.utility.Common.RegexStrings;
import vcp.client.utility.Common.UserNotification;
import vcp.client.utility.UtilityHelper;
import vcp.enums.PopUpMessageType;
import vcp.enums.Screen;

/**
 * <p>
 * user interface regular check in view class
 * </p>
 * 
 * @author VCP team
 * 
 */
public class UIRegularCheckInView extends UIAbstractView
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JFormattedTextField ftfEmail;
	private JButton btnSubmit;
	private JButton btnCancel;
	private CustomJSpinnerDate spDate;

	private JLabel siteName;

	/**
	 * getter
	 * 
	 * @return site name
	 */
	private JLabel getSiteName ()
	{
		if ( this.siteName == null )
		{
			this.siteName = new JLabel();
		}
		return this.siteName;
	}

	/**
	 * setter
	 * 
	 * @param siteName
	 */
	public void setSiteName ( String siteName )
	{
		siteName = String.format("Site Name:%s", siteName);
		this.siteName = new JLabel(siteName);
		this.siteName.setFont(new Font("Lucida Grande", Font.BOLD, 12));
	}

	private IValidationListener validationListener;

	/**
	 * setter
	 * 
	 * @param validationListener
	 */
	public void setValidationListener ( IValidationListener validationListener )
	{
		this.validationListener = validationListener;
	}

	private ValidationLayerUI emailValidationLayerUI;

	/**
	 * constructor
	 */
	public UIRegularCheckInView ()
	{
		super();
		this.setPreferredSize(new Dimension(1024, 628));

		this.resetView();
	}

	/**
	 * method updates date and time
	 * 
	 * @param requestCurrentClientDateTime
	 */
	public void updateDateTimes ( DateTime requestCurrentClientDateTime )
	{
		this.spDate.resetModel(requestCurrentClientDateTime);
	}

	/****************************************************************************************************************************
	 *** UIAbstractView methods ***
	 ****************************************************************************************************************************/
	@Override
	public ArrayList <JComponent> getAdditionalToolbarComponents ()
	{
		this.getAdditionalComponents().clear();
		this.getAdditionalComponents().add(this.getSiteName());

		return this.getAdditionalComponents();
	}

	@Override
	public void clearView ()
	{
		this.ftfEmail.setText("");
	}

	@Override
	public void resetView ()
	{
		this.btnSubmit.setEnabled(false);
	}

	@Override
	protected void setupLayout ()
	{
		this.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(100, 10));
		this.add(panel, BorderLayout.WEST);

		JPanel panel_1 = new JPanel();
		panel_1.setPreferredSize(new Dimension(100, 10));
		this.add(panel_1, BorderLayout.EAST);
		panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JPanel panel_2 = new JPanel();
		this.add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(new GridLayout(2, 0, 0, 0));

		JPanel panel_3 = new JPanel();
		panel_2.add(panel_3);
		panel_3.setLayout(new GridLayout(3, 0, 0, 0));

		JPanel panel_7 = new JPanel();
		panel_3.add(panel_7);

		JPanel panel_8 = new JPanel();
		panel_3.add(panel_8);
		panel_8.setLayout(new GridLayout(2, 0, 0, 0));

		JPanel panel_9 = new JPanel();
		panel_8.add(panel_9);
		panel_9.setLayout(new BorderLayout(0, 0));

		JPanel panel_11 = new JPanel();
		FlowLayout fl_panel_11 = (FlowLayout) panel_11.getLayout();
		fl_panel_11.setAlignment(FlowLayout.RIGHT);
		panel_11.setPreferredSize(new Dimension(275, 10));
		panel_9.add(panel_11, BorderLayout.WEST);

		JLabel lblLeavingTime = new JLabel("Leaving Time:");
		panel_11.add(lblLeavingTime);
		lblLeavingTime.setFont(new Font("Lucida Grande", Font.BOLD, 32));

		JPanel panel_16 = new JPanel();
		FlowLayout fl_panel_16 = (FlowLayout) panel_16.getLayout();
		fl_panel_16.setHgap(0);
		fl_panel_16.setAlignment(FlowLayout.LEFT);
		fl_panel_16.setVgap(0);
		panel_9.add(panel_16, BorderLayout.CENTER);

		this.spDate = new CustomJSpinnerDate(new DateTime(), this);
		panel_16.add(this.spDate);
		this.spDate.setFont(new Font("Lucida Grande", Font.PLAIN, 32));

		JPanel panel_10 = new JPanel();
		panel_8.add(panel_10);
		panel_10.setLayout(new BorderLayout(0, 0));

		JPanel panel_13 = new JPanel();
		panel_13.setPreferredSize(new Dimension(275, 10));
		FlowLayout fl_panel_13 = (FlowLayout) panel_13.getLayout();
		fl_panel_13.setAlignment(FlowLayout.RIGHT);
		panel_10.add(panel_13, BorderLayout.WEST);

		JLabel lblEmail = new JLabel("E-mail:");
		panel_13.add(lblEmail);
		lblEmail.setFont(new Font("Lucida Grande", Font.BOLD, 32));

		JPanel panel_12 = new JPanel();
		FlowLayout fl_panel_12 = (FlowLayout) panel_12.getLayout();
		fl_panel_12.setHgap(0);
		fl_panel_12.setAlignment(FlowLayout.LEFT);
		fl_panel_12.setVgap(0);
		panel_10.add(panel_12, BorderLayout.CENTER);

		this.ftfEmail = new JFormattedTextField();
		JLayer <JTextComponent> emailLayer = new JLayer <JTextComponent>(this.ftfEmail);
		this.emailValidationLayerUI = new ValidationLayerUI();
		emailLayer.setUI(this.emailValidationLayerUI);
		this.ftfEmail.setToolTipText("$$$@$$$.$$$");
		this.ftfEmail.setPreferredSize(new Dimension(292, 55));
		panel_12.add(emailLayer);
		this.ftfEmail.setFont(new Font("Lucida Grande", Font.PLAIN, 32));

		JPanel panel_4 = new JPanel();
		panel_2.add(panel_4);
		panel_4.setLayout(new GridLayout(4, 0, 0, 0));

		JPanel panel_5 = new JPanel();
		panel_4.add(panel_5);
		panel_5.setLayout(new GridLayout(0, 3, 0, 0));

		JPanel panel_14 = new JPanel();
		FlowLayout fl_panel_14 = (FlowLayout) panel_14.getLayout();
		fl_panel_14.setAlignment(FlowLayout.RIGHT);
		panel_5.add(panel_14);

		this.btnCancel = new JButton(Common.BTN_CANCEL);
		this.btnCancel.setFocusable(false);
		this.btnCancel.setPreferredSize(new Dimension(175, 75));
		panel_14.add(this.btnCancel);
		this.btnCancel.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
		this.btnCancel.setName(Common.BTN_CANCEL);

		JPanel panel_6 = new JPanel();
		panel_5.add(panel_6);

		JPanel panel_15 = new JPanel();
		FlowLayout fl_panel_15 = (FlowLayout) panel_15.getLayout();
		fl_panel_15.setAlignment(FlowLayout.LEFT);
		panel_5.add(panel_15);

		this.btnSubmit = new JButton(Common.BTN_SUBMIT);
		this.btnSubmit.setPreferredSize(new Dimension(175, 75));
		panel_15.add(this.btnSubmit);
		this.btnSubmit.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
		this.btnSubmit.setName(Common.BTN_SUBMIT);
	}

	@Override
	protected void setupListeners ()
	{
		this.btnCancel.addActionListener(this);
		this.btnSubmit.addActionListener(this);

		// this.spDate.addChangeListener(new ChangeListener()
		// {
		// @Override
		// public void stateChanged ( ChangeEvent e )
		// {
		// JSpinner spinner = (JSpinner) e.getSource();
		// // ((Date) spinner.getPreviousValue()).
		//
		// DateTime spinnerDate = new DateTime((Date)
		// spinner.getModel().getNextValue());
		// DateTime currentTime = new DateTime();
		// System.out.println(spinnerDate.toString());
		// System.out.println(currentTime.toString());
		// if ( DateTimeComparator.getDateOnlyInstance().compare(spinnerDate,
		// null) == - 1 )
		// {
		// spinner.getModel().setValue(currentTime.toDate());
		// }
		// else
		// {
		// System.out.println("spinner hour:" + spinnerDate.getHourOfDay() +
		// "|||current hour:" + currentTime.getHourOfDay());
		// if ( spinnerDate.getHourOfDay() < currentTime.getHourOfDay() )
		// {
		// spinner.getModel().setValue(currentTime.toDate());
		// return;
		// }
		// System.out.println("spinner minute:" + spinnerDate.getMinuteOfHour()
		// + "|||current minute:" + currentTime.getMinuteOfHour());
		// if ( spinnerDate.getMinuteOfHour() < currentTime.getMinuteOfHour() )
		// {
		// spinner.getModel().setValue(currentTime.toDate());
		// return;
		// }
		// else
		// {
		// // spinner.get
		// DateTime roundedTime = currentTime.withMinuteOfHour(( (
		// currentTime.getMinuteOfHour() / 10 ) + 1 ) * 10);
		// System.out.println("new time:" + roundedTime.toString());
		// spinner.getModel().setValue(roundedTime.toDate());
		// }
		// }
		// }
		// });

		// this.spTime.addChangeListener(new ChangeListener()
		// {
		// @Override
		// public void stateChanged ( ChangeEvent e )
		// {
		// JSpinner spinner = (JSpinner) e.getSource();
		//
		// Date spinnerDate = (Date) spinner.getModel().getValue();
		// Date previousDate = (Date) spinner.getModel().getPreviousValue();
		//
		// boolean needRestrict = previousDate.compareTo(spinnerDate) > 0;
		//
		// if ( needRestrict )
		// {
		// spinner.getModel().setValue(Calendar.getInstance().getTime());
		// }
		// }
		// });

		this.ftfEmail.addKeyListener(new KeyListener()
			{
				@Override
				public void keyTyped ( KeyEvent e )
				{
				}

				@Override
				public void keyPressed ( KeyEvent e )
				{

				}

				@Override
				public void keyReleased ( KeyEvent e )
				{
					String fieldEmailText = UIRegularCheckInView.this.ftfEmail.getText();

					boolean result = ! UtilityHelper.getUtilityHelperInstance().isStringNullOrEmpty(fieldEmailText);
					UIRegularCheckInView.this.btnSubmit.setEnabled(result);
				}

			});

		Pattern emailPattern = UIRootController.DEBUG
														? Pattern.compile(RegexStrings.ACCEPT_ALL)
														: Pattern.compile(RegexStrings.EMAIL);
		this.ftfEmail.setInputVerifier(new CustomInputVerifier(this.emailValidationLayerUI, emailPattern));
	}

	@Override
	protected void setupButtonsIcons ()
	{
		int iconTextGap = 20;

		ImageIcon icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_SUBMIT, 46, 46);
		if ( icon != null )
		{
			this.btnSubmit.setIcon(icon);
			this.btnSubmit.setIconTextGap(iconTextGap);
		}

		icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_CANCEL, 46, 46);
		if ( icon != null )
		{
			this.btnCancel.setIcon(icon);
			this.btnCancel.setIconTextGap(iconTextGap);
		}
	}

	/****************************************************************************************************************************
	 *** ActionListener methods ***
	 ****************************************************************************************************************************/
	@Override
	public void actionPerformed ( ActionEvent actionEvent )
	{
		JButton regularCheckInViewButton = (JButton) actionEvent.getSource();

		if ( regularCheckInViewButton.getName().equalsIgnoreCase(Common.BTN_CANCEL) )
		{
			if ( this.controllerDelegate != null )
			{
				this.controllerDelegate.onButtonPress(Screen.ONSITE);
			}
		}
		else
		{
			if ( this.validationListener != null )
			{
				if ( this.ftfEmail.getInputVerifier().verify(this.ftfEmail) )
				{
					String email = this.ftfEmail.getText();
					DateTime date = new DateTime(this.spDate.getValue());
					this.validationListener.onValidationRequest(new ValidationDescriptionEvent(this, email, date));
				}
				else
				{
					String infoMessage = String.format(UserNotification.NOT_CORRECT_FORMAT_FOR__VALUE__, this.ftfEmail.getToolTipText());
					this.controllerDelegate.showPopUpMessage(infoMessage, PopUpMessageType.INFORMATION);
				}
			}
		}
	}

}
